package com.h2cloud.controller;

import com.alibaba.fastjson.JSON;
import com.h2cloud.entity.BaseResp;
import com.h2cloud.entity.UserInfo;
import com.h2cloud.service.UserInfoService;
import com.h2cloud.service.impl.UserInfoServiceImpl;
import com.h2cloud.utils.JWTUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * (UserInfo)表控制层
 *
 * @author makejava
 * @since 2020-09-17 14:04:02
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Resource
    private UserInfoService Service;

    @PostMapping("/login")
    @ResponseBody
    public Object login(@RequestBody UserInfo userInfo) {
        System.out.println("登录");

        BaseResp baseResp = new BaseResp();
        List<UserInfo> infoList = Service.queryByAccount(userInfo.getAccount());

        if ("".equals(userInfo.getAccount()) || "".equals(userInfo.getPassword())) {
            baseResp.setCode(BaseResp.ACCOUNT_OR_PASSWORD_NULL);
            baseResp.setMsg(BaseResp.ACCOUNT_OR_PASSWORD_NULL_MSG);

        } else if (infoList.size() == 0) {
            baseResp.setCode(BaseResp.NO_ACCOUNT);
            baseResp.setMsg(BaseResp.NO_ACCOUNT_MSG);

        } else if (!userInfo.getPassword().equals(infoList.get(0).getPassword())) {
            baseResp.setCode(BaseResp.PASSWORD_ERROR);
            baseResp.setMsg(BaseResp.PASSWORD_ERROR_MSG);

        } else {
            baseResp.setCode(BaseResp.LOGIN_SUCCESS);
            baseResp.setMsg(BaseResp.LOGIN_SUCCESS_MSG);

            UserInfo userData = infoList.get(0);
            userData.setPassword(null);
            Map<String, String> map = new HashMap<>();
            map.put("username", infoList.get(0).getUsername());
            map.put("id", infoList.get(0).getId() + "");
            String token = JWTUtil.getToken(map);

            Map<String, Object> result = new HashMap<>();
            result.put("token", token);
            result.put("userInfo", JSON.toJSONString(userData));
            baseResp.setData(result);

        }
        return baseResp;
    }

    @PostMapping("/register")
    @ResponseBody
    public Object register(@RequestBody UserInfo userInfo) {
        System.out.println("注册");

        BaseResp baseResp = new BaseResp();
        List<UserInfo> infoList = Service.queryByAccount(userInfo.getAccount());

        if ("".equals(userInfo.getAccount()) || "".equals(userInfo.getUsername()) || "".equals(userInfo.getPassword())) {
            baseResp.setCode(BaseResp.ACCOUNT_OR_PASSWORD_NULL);
            baseResp.setMsg(BaseResp.ACCOUNT_OR_PASSWORD_NULL_MSG);

        } else if (infoList.size() > 0) {
            baseResp.setCode(BaseResp.HAS_ACCOUNT);
            baseResp.setMsg(BaseResp.HAS_ACCOUNT_MSG);

        } else {
            baseResp.setCode(BaseResp.REGISTER_SUCCESS);
            baseResp.setMsg(BaseResp.REGISTER_SUCCESS_MSG);
            baseResp.setData(Service.insert(userInfo));

        }

        return JSON.toJSONString(baseResp);
    }

    @PostMapping("/updataUserInfo")
    @ResponseBody
    public Object updataUserInfo(@RequestBody UserInfo userInfo, HttpServletRequest request) {
        System.out.println("/updataUserInfo");

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();
        userInfo.setId(Integer.parseInt(userid));

        Service.update(userInfo);

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMsg("请重新登录");

        return "修改完成";
    }

    @GetMapping("/getAllUserInfo")
    @ResponseBody
    public Object getAllUserInfo() {
        System.out.println("/getAllUserInfo");

        List<UserInfo> infos = Service.queryUserList();
        Set<UserInfo> set = new HashSet();
        for (int i = 0; i < infos.size(); i++) {
            infos.get(i).setPassword(null);
            set.add(infos.get(i));
        }

        return set;
    }


    @GetMapping("/getUserInfo/{userid}")
    @ResponseBody
    public Object getUserInfo(@PathVariable("userid") Integer userid) {
        System.out.println("/getUserInfo:  " + userid);

        UserInfo userInfo = Service.queryById(userid);
        userInfo.setPassword(null);

        return userInfo;
    }

    @GetMapping("/hasLogin")
    public Object hasLogin() {
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMsg("检查是否登录");
        return baseResp;
    }
}
